//
//  MembershipPolicyDetailView.swift
//  CMB250716
//
//  Created by Trae AI on 2025/01/16.
//

import SwiftUI

struct MembershipPolicyDetailView: View {
    @EnvironmentObject var dataManager: DataManager
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        VStack(spacing: 0) {
            // 顶部导航栏
            HStack {
                Button(action: {
                    presentationMode.wrappedValue.dismiss()
                }) {
                    Image(systemName: "chevron.left")
                        .font(.system(size: 18, weight: .semibold))
                        .foregroundColor(.white)
                }
                
                Text(dataManager.currentLanguage == "zh" ? "会员制度与积分使用规则" : "Membership & Points Program Guide")
                    .font(.system(size: 16, weight: .semibold, design: .rounded))
                    .foregroundColor(.white)
                    .lineLimit(1)
                
                Spacer()
            }
            .padding(.horizontal, 20)
            .padding(.top, 10)
            .padding(.bottom, 15)
            
            ScrollView {
                VStack(spacing: 10) {
                    
                    // 会员等级制度
                    MembershipLevelsSection(
                        dataManager: dataManager
                    )
                    
                    // 积分获取方式
                    PolicySection(
                        icon: "target",
                        iconColor: Color(red: 0.3, green: 0.8, blue: 1.0),
                        title: dataManager.currentLanguage == "zh" ? "积分获取方式" : "Earning Points",
                        content: [
                            "• " + (dataManager.currentLanguage == "zh" ? "消费：每消费 $1 得 1 积分" : "Spending: Earn 1 point for every $1 spent"),
                            "• " + (dataManager.currentLanguage == "zh" ? "社交互动（每日上限 45 分，每月上限 800 分）" : "Social Interactions (Daily limit: 45 points, Monthly limit: 800 points)"),
                            "  - " + (dataManager.currentLanguage == "zh" ? "点赞/评论：2分" : "Like/Comment: 2 points"),
                            "  - " + (dataManager.currentLanguage == "zh" ? "分享内容：3分" : "Share content: 3 points"),
                            "  - " + (dataManager.currentLanguage == "zh" ? "原创内容：10分" : "Original content: 10 points"),
                            "• " + (dataManager.currentLanguage == "zh" ? "邀请好友注册：双方各得 3元现金礼券" : "Invite friends: Both parties get $3 cash voucher")
                        ]
                    )
                    
                    // 礼券与积分使用说明
                    PolicySection(
                        icon: "ticket.fill",
                        iconColor: Color(red: 1.0, green: 0.4, blue: 0.6),
                        title: dataManager.currentLanguage == "zh" ? "礼券与积分使用说明" : "Voucher & Points Usage",
                        content: [
                            "• " + (dataManager.currentLanguage == "zh" ? "积分仅可用于兑换消费抵扣" : "Points can only be used for purchase discounts"),
                            "• " + (dataManager.currentLanguage == "zh" ? "折扣需以 $5 为单位兑换" : "Discounts must be redeemed in $5 increments"),
                            "• " + (dataManager.currentLanguage == "zh" ? "抵扣不可兑换现金，不可找零" : "Discounts cannot be exchanged for cash or change"),
                            "• " + (dataManager.currentLanguage == "zh" ? "礼券不可叠加使用，不可与其他活动同时使用" : "Vouchers cannot be combined or used with other promotions")
                        ]
                    )
                    
                    // 规则与条款
                    PolicySection(
                        icon: "doc.text.fill",
                        iconColor: Color(red: 1.0, green: 0.6, blue: 0.2),
                        title: dataManager.currentLanguage == "zh" ? "规则与条款" : "Terms & Conditions",
                        content: [
                            "• " + (dataManager.currentLanguage == "zh" ? "所有积分仅限本人使用，不得转让" : "All points are for personal use only and cannot be transferred"),
                            "• " + (dataManager.currentLanguage == "zh" ? "每月社交互动积分最高 800 分，超出部分不予累计" : "Monthly social interaction points capped at 800, excess not accumulated"),
                            "• " + (dataManager.currentLanguage == "zh" ? "本平台保留调整会员制度与积分比例的权利，无需另行通知" : "Platform reserves the right to adjust membership system and point ratios without notice"),
                            "• " + (dataManager.currentLanguage == "zh" ? "礼券与积分不可在特殊套餐、指定日期使用（详见门店说明）" : "Vouchers and points cannot be used for special packages or designated dates (see store details)")
                        ]
                    )
                    
                    // 权利声明
                    PolicySection(
                        icon: "exclamationmark.triangle.fill",
                        iconColor: .orange,
                        title: dataManager.currentLanguage == "zh" ? "权利声明" : "Disclaimers",
                        content: [
                            "• " + (dataManager.currentLanguage == "zh" ? "Cu Mr Bull Pte Ltd 保留对会员制度、积分兑换、活动内容的最终解释权。" : "Cu Mr Bull Pte Ltd reserves the final interpretation rights for membership system, point redemption, and event content."),
                            "• " + (dataManager.currentLanguage == "zh" ? "如遇系统故障、滥用行为、恶意刷分等情况，平台有权调整、取消相关积分与等级。" : "In case of system failures, abuse, or malicious point farming, the platform has the right to adjust or cancel related points and levels.")
                        ]
                    )
                    // 底部同意按钮
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Text("I have read and agree to the Membership Terms & Conditions.")
                            .font(.system(size: 14, weight: .medium))
                            .foregroundColor(.white)
                            .multilineTextAlignment(.center)
                            .padding(.vertical, 16)
                            .padding(.horizontal, 20)
                            .frame(maxWidth: .infinity)
                            .background(
                                LinearGradient(
                                    colors: [
                                        Color(red: 1.0, green: 0.6, blue: 0.2),
                                        Color(red: 1.0, green: 0.4, blue: 0.1)
                                    ],
                                    startPoint: .topLeading,
                                    endPoint: .bottomTrailing
                                )
                            )
                            .cornerRadius(12)
                    }
                    .padding(.top, 10)
                }
                .padding(.horizontal, 20)
                .padding(.bottom, 30)
            }
        }
        .background(
            LinearGradient(
                colors: [
                    Color(red: 0.05, green: 0.05, blue: 0.05),
                    Color(red: 0.08, green: 0.08, blue: 0.08),
                    Color(red: 0.12, green: 0.10, blue: 0.09)
                ],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
        )
    }
}

struct PolicySection: View {
    let icon: String
    let iconColor: Color
    let title: String
    let content: [String]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 标题
            HStack(spacing: 12) {
                ZStack {
                    Circle()
                        .fill(
                            RadialGradient(
                                colors: [
                                    iconColor.opacity(0.3),
                                    iconColor.opacity(0.1),
                                    Color.clear
                                ],
                                center: .center,
                                startRadius: 5,
                                endRadius: 20
                            )
                        )
                        .frame(width: 40, height: 40)
                        .blur(radius: 4)
                    
                    Circle()
                        .fill(
                            LinearGradient(
                                colors: [
                                    iconColor,
                                    iconColor.opacity(0.8)
                                ],
                                startPoint: .topLeading,
                                endPoint: .bottomTrailing
                            )
                        )
                        .frame(width: 29, height: 29)
                        .overlay(
                            Image(systemName: icon)
                                .font(.system(size: 14, weight: .bold))
                                .foregroundColor(.white)
                        )
                }
                
                Text(title)
                    .font(.system(size: 20, weight: .bold, design: .rounded))
                    .foregroundColor(.white)
            }
            
            // 内容
            VStack(alignment: .leading, spacing: 8) {
                ForEach(content, id: \.self) { item in
                    if item.hasPrefix("•") {
                        HStack(alignment: .top, spacing: 0) {
                            Text("•")
                                .font(.system(size: 13, weight: .medium))
                                .foregroundColor(.white.opacity(0.9))
                                .frame(width: 12, alignment: .leading)
                            
                            Text(String(item.dropFirst(2)))
                                .font(.system(size: 13, weight: .medium))
                                .foregroundColor(.white.opacity(0.9))
                                .lineLimit(nil)
                                .multilineTextAlignment(.leading)
                                .frame(maxWidth: .infinity, alignment: .leading)
                                .fixedSize(horizontal: false, vertical: true)
                        }
                    } else {
                        Text(item)
                            .font(.system(size: 13, weight: .medium))
                            .foregroundColor(.white.opacity(0.9))
                            .lineLimit(nil)
                            .multilineTextAlignment(.leading)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .fixedSize(horizontal: false, vertical: true)
                    }
                }
            }
        }
        .padding(20)
        .background(
            ZStack {
                // 主背景
                RoundedRectangle(cornerRadius: 18)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color(red: 0.15, green: 0.15, blue: 0.20),
                                Color(red: 0.10, green: 0.10, blue: 0.15),
                                Color(red: 0.08, green: 0.08, blue: 0.12)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                
                // 发光边框
                RoundedRectangle(cornerRadius: 18)
                    .stroke(
                        LinearGradient(
                            colors: [
                                iconColor.opacity(0.4),
                                iconColor.opacity(0.2),
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 1.5
                    )
                
                // 内部高光
                RoundedRectangle(cornerRadius: 18)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.08),
                                Color.clear,
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
            }
        )
        .shadow(color: iconColor.opacity(0.2), radius: 8, x: 0, y: 4)
        .shadow(color: Color.black.opacity(0.3), radius: 12, x: 0, y: 6)
    }
}

struct MembershipLevelsSection: View {
    let dataManager: DataManager
    
    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 标题
            HStack(spacing: 12) {
                ZStack {
                    Circle()
                        .fill(
                            RadialGradient(
                                colors: [
                                    Color(red: 1.0, green: 0.8, blue: 0.0).opacity(0.3),
                                    Color(red: 1.0, green: 0.8, blue: 0.0).opacity(0.1),
                                    Color.clear
                                ],
                                center: .center,
                                startRadius: 5,
                                endRadius: 20
                            )
                        )
                        .frame(width: 40, height: 40)
                        .blur(radius: 4)
                    
                    Circle()
                        .fill(
                            LinearGradient(
                                colors: [
                                    Color(red: 1.0, green: 0.8, blue: 0.0),
                                    Color(red: 1.0, green: 0.8, blue: 0.0).opacity(0.8)
                                ],
                                startPoint: .topLeading,
                                endPoint: .bottomTrailing
                            )
                        )
                        .frame(width: 29, height: 29)
                        .overlay(
                            Image(systemName: "crown.fill")
                                .font(.system(size: 14, weight: .bold))
                                .foregroundColor(.white)
                        )
                }
                
                Text(dataManager.currentLanguage == "zh" ? "会员等级制度" : "Membership Levels")
                    .font(.system(size: 20, weight: .bold, design: .rounded))
                    .foregroundColor(.white)
            }
            
            // 表格内容
            VStack(spacing: 12) {
                // 表头
                HStack {
                    Text(dataManager.currentLanguage == "zh" ? "等级" : "Level")
                        .font(.system(size: 12, weight: .bold))
                        .foregroundColor(.white.opacity(0.8))
                        .frame(maxWidth: .infinity, alignment: .leading)
                    
                    Text(dataManager.currentLanguage == "zh" ? "要求积分" : "Points Required")
                        .font(.system(size: 12, weight: .bold))
                        .foregroundColor(.white.opacity(0.8))
                        .frame(maxWidth: .infinity, alignment: .center)
                    
                    Text(dataManager.currentLanguage == "zh" ? "兑换比例" : "Exchange Rate")
                        .font(.system(size: 12, weight: .bold))
                        .foregroundColor(.white.opacity(0.8))
                        .frame(maxWidth: .infinity, alignment: .trailing)
                }
                .padding(.bottom, 8)
                
                Divider()
                    .background(.white.opacity(0.3))
                
                // 表格行
                VStack(spacing: 8) {
                    MembershipLevelRow(
                        level: "Level 1",
                        name: dataManager.currentLanguage == "zh" ? "蓝调萌牛" : "Neon Rookie",
                        points: dataManager.currentLanguage == "zh" ? "起始等级" : "Starting",
                        rate: "100分 = $5"
                    )
                    
                    MembershipLevelRow(
                        level: "Level 2",
                        name: dataManager.currentLanguage == "zh" ? "橙燃玩家" : "Hype Blaze",
                        points: "201+",
                        rate: "80分 = $5"
                    )
                    
                    MembershipLevelRow(
                        level: "Level 3",
                        name: dataManager.currentLanguage == "zh" ? "紫域主理人" : "Vibe Regent",
                        points: "801+",
                        rate: "50分 = $5"
                    )
                    
                    MembershipLevelRow(
                        level: "Level 4",
                        name: dataManager.currentLanguage == "zh" ? "金耀传说" : "Aureus Legend",
                        points: "1601+",
                        rate: "30分 = $5"
                    )
                }
                
                Divider()
                    .background(.white.opacity(0.3))
                    .padding(.top, 8)
                
                Text(dataManager.currentLanguage == "zh" ? "每消费S$1累计1分" : "Earn 1 point for every S$1 spent")
                    .font(.system(size: 13, weight: .medium))
                    .foregroundColor(.white.opacity(0.9))
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding(.top, 4)
            }
        }
        .padding(20)
        .background(
            ZStack {
                // 主背景
                RoundedRectangle(cornerRadius: 18)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color(red: 0.15, green: 0.15, blue: 0.20),
                                Color(red: 0.10, green: 0.10, blue: 0.15),
                                Color(red: 0.08, green: 0.08, blue: 0.12)
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
                
                // 边框
                RoundedRectangle(cornerRadius: 18)
                    .stroke(
                        LinearGradient(
                            colors: [
                                Color(red: 1.0, green: 0.8, blue: 0.0).opacity(0.4),
                                Color(red: 1.0, green: 0.8, blue: 0.0).opacity(0.2),
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        ),
                        lineWidth: 1.5
                    )
                
                // 高光效果
                RoundedRectangle(cornerRadius: 18)
                    .fill(
                        LinearGradient(
                            colors: [
                                Color.white.opacity(0.08),
                                Color.clear,
                                Color.clear
                            ],
                            startPoint: .topLeading,
                            endPoint: .bottomTrailing
                        )
                    )
            }
        )
        .shadow(color: Color(red: 1.0, green: 0.8, blue: 0.0).opacity(0.2), radius: 8, x: 0, y: 4)
        .shadow(color: Color.black.opacity(0.3), radius: 12, x: 0, y: 6)
    }
}

struct MembershipLevelRow: View {
    let level: String
    let name: String
    let points: String
    let rate: String
    
    var body: some View {
        HStack {
            VStack(alignment: .leading, spacing: 2) {
                Text(level)
                    .font(.system(size: 12, weight: .semibold))
                    .foregroundColor(.white)
                Text(name)
                    .font(.system(size: 10, weight: .medium))
                    .foregroundColor(.white.opacity(0.7))
            }
            .frame(maxWidth: .infinity, alignment: .leading)
            
            Text(points)
                .font(.system(size: 12, weight: .medium))
                .foregroundColor(.white.opacity(0.9))
                .frame(maxWidth: .infinity, alignment: .center)
            
            Text(rate)
                .font(.system(size: 12, weight: .medium))
                .foregroundColor(.white.opacity(0.9))
                .frame(maxWidth: .infinity, alignment: .trailing)
        }
        .padding(.vertical, 4)
    }
}

#Preview {
    MembershipPolicyDetailView()
        .environmentObject(DataManager.shared)
}